package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.R;

public class TimelineView extends RelativeLayout {

    private static final int TOP_MARGIN = 24;

    private ImageView mIvPoint;

    private RelativeLayout mRlDate;

    private CuteTextView mCtvDate;

    private CuteTextView mCtvCount;

    private CuteTextView mCtvRole;

    private CuteTextView mCtvTime;

    private FrameLayout mFlContent;

    private FrameLayout mFlLine;

    private int y;

    public TimelineView(Context context) {
        super(context);
        init(context);
    }

    public TimelineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TimelineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void setDateVisibility(boolean show, boolean top) {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int topMargin = 0;
        if (show) {
            mIvPoint.setVisibility(VISIBLE);
            mRlDate.setVisibility(VISIBLE);
            if (top) {
                topMargin = (int) (TOP_MARGIN * metrics.density);
            } else {
                topMargin = 0;
            }
            LayoutParams params = (LayoutParams) mFlLine.getLayoutParams();
            params.setMargins(params.leftMargin, topMargin, params.rightMargin, params.bottomMargin);
            mFlLine.setLayoutParams(params);
            y = (int) (48 * metrics.density);
        } else {
            mIvPoint.setVisibility(GONE);
            mRlDate.setVisibility(GONE);
            LayoutParams params = (LayoutParams) mFlLine.getLayoutParams();
            params.setMargins(params.leftMargin, topMargin, params.rightMargin, params.bottomMargin);
            mFlLine.setLayoutParams(params);
            y = 0;
        }
    }

    public void setTimeVisibility(boolean show) {
        if (show) {
            mCtvTime.setVisibility(View.VISIBLE);
        } else {
            mCtvTime.setVisibility(View.INVISIBLE);
        }
    }

    public void setDate(String date) {
        mCtvDate.setText(date);
    }

    public void setCount(String count) {
        mCtvCount.setText(count);
    }

    public void setRole(String role) {
        mCtvRole.setText(role);
    }

    public void setTime(String time) {
        mCtvTime.setText(time);
    }

    public FrameLayout getContent() {
        return mFlContent;
    }

    public int getTimelineY() {
        return y;
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_timeline, this);
        mIvPoint = (ImageView) findViewById(R.id.point);
        mRlDate = (RelativeLayout) findViewById(R.id.date_layout);
        mCtvDate = (CuteTextView) findViewById(R.id.select_date);
        mCtvCount = (CuteTextView) findViewById(R.id.count);
        mCtvRole = (CuteTextView) findViewById(R.id.role);
        mCtvTime = (CuteTextView) findViewById(R.id.time);
        mFlContent = (FrameLayout) findViewById(R.id.content);
        mFlLine = (FrameLayout) findViewById(R.id.line);
    }
}
